import 'package:flutter/material.dart';





class MyGetNet extends StatefulWidget {

  @override
  State<MyGetNet> createState() {
    // TODO: implement createState
    return new MyNet();
  }

}

class MyNet extends State<MyGetNet> with TickerProviderStateMixin{

  final List<ChatMessage> _message=<ChatMessage>[];


  final TextEditingController _textController = new TextEditingController(); 


  @override
  void dispose() {
    // TODO: implement dispose
    for(ChatMessage message in _message){
      message.animationController.dispose();
    }
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar:new AppBar(
        title: new Text("网络测试页面"),
      ),
      body: new Column(
        children: <Widget>[
          new Flexible(
              child: new ListView.builder(padding:new EdgeInsets.all(8.0),
                  reverse: true,
                  itemBuilder: (_,int index)=>  _message[index],
                  itemCount: _message.length,
              ),),
          new Divider(height: 1.0,),
          new Container(
            decoration: new BoxDecoration(
              color: Theme.of(context).cardColor,
            ),
            child: _buildTextComposer(),
          ),
        ],
      )
    );
  }

  Widget _buildTextComposer(){
    return new IconTheme(data: new IconThemeData(color: Theme
        .of(context)
        .accentColor),
    child: new Container(
      padding: const EdgeInsets.only(top: 20.0),
      margin: const EdgeInsets.symmetric(horizontal: 8.0),
      child:new Row(
        children: <Widget>[
          new Flexible(
            child: new TextField(
            controller: _textController,
            onSubmitted: _handleSubmitted,
            decoration: new InputDecoration.collapsed(hintText: "发送命令"),
          ),),
          new Container(
            margin: new EdgeInsets.symmetric( horizontal: 4.0) ,
            child: new IconButton(icon: new Icon(Icons.send),
                onPressed: () => _handleSubmitted(_textController.text)),
          )
        ],
      )
    ));
  }

  void _handleSubmitted(String text) {
    _textController.clear();
    ChatMessage message=new ChatMessage(text: text,
    animationController: new AnimationController(duration: new Duration(milliseconds: 700),
        vsync: this,),
    );
    setState(() {
      _message.insert(0, message);
    });
    message.animationController.forward();
  }
}
const String _name = "Your Name";



class ChatMessage extends StatelessWidget{
  final String text;
  final AnimationController animationController;

  ChatMessage({this.text,this.animationController});


  @override
  Widget build(BuildContext context) {
    return   new SizeTransition(sizeFactor: new CurvedAnimation(parent: animationController, curve: Curves.easeOut),
      axisAlignment: 0.0,
      child:     new Container(
        margin: const EdgeInsets.symmetric(vertical: 10.0),
    child: new Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
      new Container(
      margin: const EdgeInsets.only(right: 16.0),
      child: new CircleAvatar(child: new Text(_name[0])),
      ),
      new Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
      new Text(_name, style: Theme
          .of(context)
          .textTheme
          .subhead),
      new Container(
      margin: const EdgeInsets.only(top: 5.0),
      child: new Text(text),
      ),
      ],
      ),
      ],
      ),
      ));
  }

}

